<?php

namespace app\admin\service;

use Qcloud\Sms\SmsSingleSender;
use think\facade\Cache;
use think\facade\Db;
use think\facade\Log;

class SmsService
{
    /**
     * 发短信
     * 1 提交办事给 村或乡管理员 发
     * 2 办事审核通过给 用户 发
     * 3 办事审核未通过给 用户 发
     * 4 办事未通过村级审核给 用户 发
     * 5 办事审核村级通过给 乡管理员 发
     * 6 办事审核村级通过给 用户 发
     * 7 办事申请乡镇未通过给 用户 发
     * 8 办事申请乡镇通过给 用户 发
     * 9 超时提前提醒 村、乡管理员 - 暂未使用
     * 10 办事已受理给 用户 发
     * 11 乡镇管理员通过给 部门 发 - 暂未使用
     * 12 惠农资金相关 - 暂未使用
     * 13 惠农资金相关 - 暂未使用
     * 14 留言发布
     * 15 留言回复
     * 16 公示公开审核相关
     */
    public static function sendMes($type,$phone,$paramData=[])
    {
        //require "../extend/Qcloud/Sms/index.php";
        $config = \app\common\service\CommonService::getSystemConfigByDomain()['config'];
        $smsSign=$config['TENCENT_MESSAGE_SIGN'];
        $appid=$config['TENCENT_MESSAGE_APPID'];
        $appkey=$config['TENCENT_MESSAGE_APPKEY'];
        $params=[];

        $ssender = new SmsSingleSender($appid,$appkey);
        $templateId = $config['TENCENT_MESSAGE_'.$type];
        if (in_array($type,[1,5,11])){
            $params=[$paramData['name'],$paramData['dead_time']];
        }elseif(in_array($type,[2,3,4,6,7,8,15,16])){
            $params=[$paramData['name']];
        }elseif(in_array($type,[10])){
            $params=[$paramData['name'],$paramData['areaName']];
        }elseif(in_array($type,[9])){
            $params=[];
        }elseif(in_array($type,[12,13,14])){
            $params=[];
        }
        $phone = array_values($phone);
        if (count($phone) > 0){
            for ($i=0;$i<count($phone);$i++){
                $phoneNumbers = $phone[$i];
                if ($phoneNumbers != '' && $phoneNumbers != null){
                    try {
                        @file_put_contents(ROOT_PATH. 'public/message.txt','手机号'.$phoneNumbers.'，参数'.implode(',',$params).'，模板'.$templateId."-------end\r\n",FILE_APPEND);
                        /*$result = $ssender->sendWithParam("86", $phoneNumbers, $templateId, $params, $smsSign, "", "");  // 签名参数未提供或者为空时，会使用默认签名发送短信
                        $rsp = json_decode($result,true);
                        if($rsp['result']==0){*/
                        if(true){
                            $info[] = $phoneNumbers.' success in send';
                        }else{
                            $info[] = $phoneNumbers.' fail in send';
                        }
                    } catch(\Exception $e) {
                        $info[] = $phoneNumbers.' fail in send,'.$e->getMessage();
                    }
                }else{
                    $info[] = 'fail in send,no phone';
                }
            }
        } else{
            $info[] = 'fail in send,no phone';
        }
        $info = implode(',',$info);
        trace($info,'info');
        return true;
    }

    //给开发者发送重启memcache服务通知
    public static function sendNoticeMes()
    {
        //require "../extend/Qcloud/Sms/index.php";
        //工程师手机号
        $phone = ['15809376063','18054123980','15393361788'];
        //使用肃北短信
        $smsSign='肃北e监督';
        $appid='1400540544';
        $appkey='d29ccb5f15a5d7e022fad4c90313ca00';
        $templateId = '1090024';
        $params=[];
        $ssender = new SmsSingleSender($appid,$appkey);

        $phone = array_values($phone);
        if (count($phone) > 0){
            for ($i=0;$i<count($phone);$i++){
                $phoneNumbers = $phone[$i];
                if ($phoneNumbers != '' && $phoneNumbers != null){
                    try {
                        //@file_put_contents(ROOT_PATH. 'public/message.txt','手机号'.$phoneNumbers.'，参数'.implode(',',$params).'，模板'.$templateId."-------end\r\n",FILE_APPEND);
                        $result = $ssender->sendWithParam("86", $phoneNumbers, $templateId, $params, $smsSign, "", "");  // 签名参数未提供或者为空时，会使用默认签名发送短信
                        $rsp = json_decode($result,true);
                        if($rsp['result']==0){
                        //if(true){
                            $info[] = $phoneNumbers.' success in send';
                        }else{
                            $info[] = $phoneNumbers.' fail in send';
                        }
                    } catch(\Exception $e) {
                        $info[] = $phoneNumbers.' fail in send,'.$e->getMessage();
                    }
                }else{
                    $info[] = 'fail in send,no phone';
                }
            }
        } else{
            $info[] = 'fail in send,no phone';
        }
        $info = implode(',',$info);
        trace($info,'info');
        return true;
    }

    /**
     * 获取部门管理员手机号
     */
    public static function getDepartmentPhone($department){
        $res = [];
        $department = intval($department);
        if ($department != 0){
            $where[] = ['department','=',$department];
            $where[] = ['phone','<>',''];
            $phoneObj = Db::connect('jiwei')->name('system_admin')->where($where)->whereNotNull('phone')->field('phone')->select();
            if (false != $phoneObj){
                $res = array_column($phoneObj->toArray(),'phone');
            }
        }
        return $res;
    }

    /**
     * 获取具体乡镇所有管理员手机号
     */
    public static function getTownAdminPhone($township){
        $res = [];
        $township = intval($township);
        if ($township != 0){
            $townshipArr = config('township');
            $model = Db::connect(str_replace('xwql_dh_','',$townshipArr[$township]['db']))->table('ea_system_admin');
            $where[] = ['phone','<>',''];
            $phoneObj = $model->where($where)->whereNotNull('phone')->field('phone')->select();
            if (false != $phoneObj){
                $res = array_column($phoneObj->toArray(),'phone');
            }
        }
        return $res;
    }

    /**
     * 获取用户手机号
     */
    public static function getUserPhone($open_id){
        $res = [];
        $where[] = ['open_id','=',$open_id];
        $phone = Db::name('wx_user')->where($where)->value('phone');
        if ($phone != null && $phone != ''){
            $res[] = $phone;
        }
        return $res;
    }



}